import axios from "axios";
import { defineStore } from "pinia";
import { nanoid } from "nanoid";

export const useTalkStore = defineStore('talk111', {
    //数据
    //function函数形式
    state() {
        return {
            // list: [
            //     {
            //         id:"01",
            //         content:"土味情话1",
            //     },
            //     {
            //         id:"02",
            //         content:"土味情话2",
            //     },
            //     {
            //         id:"03",
            //         content:"土味情话3",
            //     },
            //     {
            //         id:"04",
            //         content:"土味情话4",
            //     },

            // ],

            //._object.list才是真正的数组
            list: JSON.parse(localStorage.getItem("TalkMain") as string) ? JSON.parse(localStorage.getItem("TalkMain") as string)._object.list : []

            //as string : 类型校验
        }
    },
    //箭头函数简化形式
    // state:() => ({
    //     sum:7
    // }),
    //方法
    actions: {
        // 模拟异步
        // asynchronous() {
        //     return new Promise((resolve)=> {
        //         setTimeout(() => {
        //             resolve('模拟异步返回值')
        //         }, 2000);
        //     })
        // },

        // 异步操作
        async getList() {
            // console.log(localStorage.getItem("TalkMain") as string);

            // const res = await this.asynchronous()

            // const res = await axios.get("https://api.uomg.com/api/rand.qinghua");

            const res = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json")

            //https://api.uomg.com/api/rand.qinghua?format=json


            console.log(res)
            let { data: { content } } = res;

            let obj = {
                id: nanoid(),
                content
            }

            this.list.push(obj)
            //再存储回本地
            // localStorage.setItem("TalkMain",JSON.stringify(this.list))

        }
    },
    //计算属性
    getters: {
        // bigSum(s){return s.sum * 10}
        // bigSum(state){return state.sum * 10}


    },



})